package com.springboot.base.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author meiguangya
 */
@Configuration
public class ElasticSearchRestClient {

    @Value("${elasticsearch.ip}")
    String ipAddress;

    @Bean("highLevelClient")
    public RestHighLevelClient restHighLevelClient(){
        String[] address=ipAddress.split(":");
        String ip = address[0];

        int port = Integer.valueOf(address[1]);
        HttpHost host = new HttpHost(ip,port);
        return new RestHighLevelClient(RestClient.builder(host));
    }
}
